sample = [[
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

        <title>Example Feed</title>
        <subtitle>Insert witty or insightful remark here</subtitle>
        <link href="http://example.org/"/>
        <updated>2003-12-13T18:30:02Z</updated>
        <author>
                <name>John Doe</name>
                <email>johndoe@example.com</email>
        </author>
        <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>

        <entry>
                <title>Atom-Powered Robots Run Amok</title>
                <link href="http://example.org/2003/12/13/atom03"/>
                <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
                <updated>2003-12-13T18:30:02Z</updated>
                <summary>Some text.</summary>
        </entry>

]]
sample2 = 
[[
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
        <channel>

                <title>Example Feed</title>
                <description>Insert witty or insightful remark here</description>
                <link>http://example.org/</link>
                <lastBuildDate>Sat, 13 Dec 2003 18:30:02 GMT</lastBuildDate>
                <managingEditor>johndoe@example.com (John Doe)</managingEditor>

                <item>
                        <title>Atom-Powered Robots Run Amok</title>
                        <link>http://example.org/2003/12/13/atom03</link>
                        <guid isPermaLink="false">urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</guid>
                        <pubDate>Sat, 13 Dec 2003 18:30:02 GMT</pubDate>
                        <description>Some text.</description>
                </item>

        </channel>
</rss>
]]
rss = {
--channel
    {title="Channel",subtitle="test channel",link="http://localhost/index.html",updated=now,author="Robert",email="robert.jay.gould@gmail.com",id="1234567890"}, 
--entries
    {title="Entry1",link="http://localhost/test1.html",id="2345678910",updated=now,summary="some summary stuff"},
    {title="Entry2",link="http://localhost/test2.html",id="2345678911",updated=now,summary="some summary stuff"},
}

--Function to fill replace all tokens within a string
function string.fillout(tokens,instring)
   local outstring = instring
   local k,v
   for k,v in pairs(tokens) do
       outstring = string.gsub(outstring, "$"..k, tostring(v))
   end
   return outstring
end

function tochannel(entry)
  local feed = 
[[
        <title>$title</title>
        <subtitle>$subtitle</subtitle>
        <link href="$link"/>
        <updated>$updated</updated>
        <author>
                <name>$author</name>
                <email>$email</email>
        </author>
        <id>$id</id>    
]]
  feed = 
[[
                <title>$title</title>
                <description>$subtitle</description>
                <link>$link</link>
                <lastBuildDate>$updated</lastBuildDate>
                <managingEditor>$email ($author)</managingEditor>
]]
  return fillout(entry,feed)
end

function toentry(entry)
  local feed = 
[[
        <entry>
                <title>$title</title>
                <link href="$link"/>
                <id>$id</id>
                <updated>$updated</updated>
                <summary>$summary</summary>
        </entry>
]]
  feed = 
[[
                <item>
                        <title>$title</title>
                        <link>$link</link>
                        <guid isPermaLink="false">$id</guid>
                        <pubDate>$updated</pubDate>
                        <description>$summary</description>
                </item>  
]]
  return fillout(entry,feed)
end

function makeentries(rss,numberofentries)
  local numberofentries = numberofentries or 10
  local entries = ""
  local index = 0
  while ((index<numberofentries) and ((#rss-index)>1)) do
    entries = entries..toentry(rss[#rss-index])
    index = index + 1
  end
  return entries
end

function makerss(rss)
  local feed = 
[[ 
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
$channel
$entries
</feed>
  ]]
  feed = 
[[
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
        <channel>
$channel

$entries
        </channel>
</rss>
]]
  --feed = "channel"
  return fillout({channel=tochannel(rss[1]),entries=makeentries(rss,10)},feed)
end

print(makerss(rss))

function tohex(s)
  for b in string.gfind(s, ".") do
    io.write(string.format("%02X ", string.byte(b)))
  end
  io.write("\n")
end


function makesafechars()
  local t = {}
  table.insert(t,".")
  table.insert(t,"-")
  --numerics
  for i = 48, 57 do
    table.insert(t,string.char(i))
  end
  --upper case
  for i = 65, 90 do
    table.insert(t,string.char(i))
  end
  --lower case
  for i = 97, 122 do
    table.insert(t,string.char(i))
    i = i + 1
  end
  return t
end

safechars = makesafechars()
for k,v in ipairs(safechars) do 
  print(k,v)
end

function tosafe(s)
  for b in string.gfind(s, ".") do
    local ri = math.floor((string.byte(b)/4))
    local r = safechars[ri] or "!"
    local l = safechars[((string.byte(b)%64))]or "!"
    io.write(string.format("%s%s ", r,l ))
  end
  io.write("\n")
end

tosafe("1234")
